package com.dxd.onlineshop.mapper;

import com.dxd.onlineshop.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface LoginMapper {
    @Select("select id,account,password,username,role,created_at from users where account=#{account} and password=#{password}")
    User login(String account, String password);

    @Select("SELECT COUNT(*) FROM users WHERE account = #{account}")
    int checkAccountExists(String account);

    @Select("SELECT COUNT(*) FROM users WHERE username = #{username}")
    int checkUsernameExists(String username);

    @Insert("INSERT INTO users (username, password, account, role, created_at, balance) " +
            "VALUES (#{username}, #{password}, #{account}, #{role}, #{createdAt}, #{balance})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int register(User user);

    @Select("SELECT id, account, username, role, created_at, balance FROM users WHERE id = #{id}")
    User getUserById(int id);
}

